package com.rjj.wlqq.jcrop;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * @author renjianjun
 * @date 2020/10/23 16:26
 * @email 215060580@qq.com
 */
public class ImageKit {

    /**
     * 高保真缩放
     */
    public static BufferedImage resize(BufferedImage bi, int toWidth, int toHeight) {
        Graphics g = null;
        try {
            // 从 BufferedImage 对象中获取一个经过缩放的 image
            Image scaledImage = bi.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH);
            // 创建 BufferedImage 对象，存放缩放过的 image
            BufferedImage ret = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
            g = ret.getGraphics();
            g.drawImage(scaledImage, 0, 0, null);
            return ret;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (g != null) {
                g.dispose();
            }
        }
    }
}
